<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Db;
/**
 * 示例接口
 */
class Demo extends Api
{

    //如果$noNeedLogin为空表示所有接口都需要登录才能请求
    //如果$noNeedRight为空表示所有接口都需要验证权限才能请求
    //如果接口已经设置无需登录,那也就无需鉴权了
    //
    // 无需登录的接口,*表示全部
    protected $noNeedLogin = ['*', '*'];
    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = ['*'];

    public function ceshi_php()
    {
        phpinfo();
    }
    public function tuisong()
    {
        jpush_stu_single('13','0000','0');
    }
    

    /**
     * 测试方法
     *
     * @ApiTitle    (测试名称)
     * @ApiSummary  (测试描述信息)
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/demo/test/id/{id}/name/{name})
     * @ApiHeaders  (name=token, type=string, required=true, description="请求的Token")
     * @ApiParams   (name="id", type="integer", required=true, description="会员ID")
     * @ApiParams   (name="name", type="string", required=true, description="用户名")
     * @ApiParams   (name="data", type="object", sample="{'user_id':'int','user_name':'string','profile':{'email':'string','age':'integer'}}", description="扩展数据")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturnParams   (name="data", type="object", sample="{'user_id':'int','user_name':'string','profile':{'email':'string','age':'integer'}}", description="扩展数据返回")
     * @ApiReturn   ({
         'code':'1',
         'msg':'返回成功'
        })
     */

    //7天后   添加供应商的订单同步
    public function gys_jiesuan_tongbu($arr=[])
    {
        $orderdetail = DB::name('orderdetail')->where('order_id',$arr['id'])->select();
        $gys_jiesuan = DB::name('gys_jiesuan');
        $goods = DB::name('goods');
        $jiesuan = [];
        foreach ($orderdetail as $key => $value) {
            $goods_info = $goods->where('id',$value['goods_id'])->field('shuilv,cost_price')->find();
            $jiesuan[$key]['supplier_id'] = $arr['supplier_id'];
            $jiesuan[$key]['orderdetail_id'] = $value['id'];
            $jiesuan[$key]['goods_id'] = $value['goods_id'];
            $jiesuan[$key]['order_id'] = $value['order_id'];
            $jiesuan[$key]['dd_money'] = $value['num'] * $value['price'];
            $jiesuan[$key]['cb_money'] = $value['num'] * $goods_info['cost_price'];
            $jiesuan[$key]['yf_money'] = $arr['freight'];
            $jiesuan[$key]['se_money'] = $value['num'] * $value['price'] *$goods_info['shuilv']/100;
            $jiesuan[$key]['count_money'] = $jiesuan[$key]['cb_money'] + $arr['freight'] + $jiesuan[$key]['se_money'];
            $jiesuan[$key]['createtime'] = time();
            $arr['freight'] = 0;

        }
        if ($arr['id'] == 655) {
            // code...
            echo 200;die;
            $gys_jiesuan->insertAll($jiesuan);
        }
    }
    

    //7天后   添加供应商的订单同步  /var/www/html/php-yc1/public/
    public function test()
    {
        $aa = '000000';
        $this->success('ok',$aa,200);
        // $order_info = DB::name('order')->where('shouhuotime','<',time()-7*86400)->select();
        // foreach ($order_info as $key => $value) {
        //       $this->gys_jiesuan_tongbu($order_info[$key]);

        // }

    }    
    public function get_version()
    {
        echo  "https://".$_SERVER['SERVER_NAME'];
        echo '<br/>';
        echo $this->request->domain();
    }
    public function test000()
    {
        $request = Request::instance();
        $id = $request->param("id");//订单详情id
        $userhome_id = $request->param("userhome_id")?:0;//用户id
        $order_id = DB::name('orderdetail')->where('id',$id)->where('userhome_id',$userhome_id)->value('order_id');

        $arr['tkyy'] = $request->param("tkyy");//退款原因
        $arr['thnum'] = $request->param("thnum");//退货数量
        $arr['tkprice'] = $request->param("tkprice");//退款金额
        $arr['remark'] = $request->param("remark");//备注
        $arr['shhimages'] = $request->param("shhimages");//售后图片路径 多张图片用,拼接
        $arr['shouhoutime'] = time();//售后时间
        $arr['is_shouhou'] = 1;
        // dump($arr);die;
        //查询订单实际支付金额 , 使用抵扣券金额 ,不包含运费(如需退运费,再加上)
        $order_zf_price = DB::name('order')->where('id',$order_id)->field('price,dikou,coupon,freight,allprice')->find();
        //总金额与退款金额的比例
        $xianjin_bili = $arr['tkprice'] / $order_zf_price['price'];
        //减去运费
        if ($order_zf_price['allprice'] > $order_zf_price['dikou']) {
            $order_zf_price['allprice'] = $order_zf_price['allprice']-$order_zf_price['freight'];
        }else{
            $order_zf_price['dikou'] = $order_zf_price['dikou']-$order_zf_price['freight'];
        }
        //判断是否全部退货
        $sum_num = DB::name('orderdetail')->where('order_id',$order_id)->sum('num');
        if ($arr['thnum'] == $sum_num) {//一单全部退完
            $arr['sjtkprice'] = $order_zf_price['allprice'] ;
            $arr['sjtdkq'] = $order_zf_price['dikou'];
        }
        //计算需要退的钱 和抵扣券
        // 判断是否是最后一个售后单
        $ysq_shouhoudan = DB::name('orderdetail')->where('order_id',$order_id)->sum('thnum');
        if ($arr['thnum'] + $ysq_shouhoudan == $sum_num) {
            $arr['sjtkprice'] = $order_zf_price['allprice'] - DB::name('orderdetail')->where('order_id',$order_id)->sum('sjtkprice');
            $arr['sjtdkq'] = $order_zf_price['dikou'] - DB::name('orderdetail')->where('order_id',$order_id)->sum('sjtdkq');
        }
        //按比例计算应退 金额 和券
        $arr['sjtkprice'] = number_format( $xianjin_bili * $order_zf_price['allprice'],2);
        $arr['sjtdkq'] = number_format( $xianjin_bili * $order_zf_price['dikou'],2);


        

        DB::name('orderdetail')->where('id',$id)->update($arr);
       
    }

    /**
     * 无需登录的接口
     *
     */
    public function test1()
    {
        $this->success('返回成功', ['action' => 'test1']);
    }

    /**
     * 需要登录的接口
     *
     */
    public function test2()
    {
        $this->success('返回成功', ['action' => 'test2']);
    }

    /**
     * 需要登录且需要验证有相应组的权限
     *
     */
    public function test3()
    {
        $this->success('返回成功', ['action' => 'test3']);
    }

    //ios  自己改版本
    public function up_bb()     //*******************勿删****************
    {
        $request = [
            'status' => $this->request->param('status'),
            'banben' => $this->request->param('banben')?:'1.0.0',

            'id' => $this->request->param('id')?:1,
        ];
        DB::name('banben')->where('id',$request['id'])->update($request);
        return 'OK,已经改好了,当前状态是:'.$request['status'].',版本是:'.$request['banben'] ;
    }

}
